package swu.edu.cn.javaee.service;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import swu.edu.cn.javaee.entity.Article;
import swu.edu.cn.javaee.entity.ArticleInfo;
import swu.edu.cn.javaee.entity.User;
import swu.edu.cn.javaee.entity.UserInfo;
import swu.edu.cn.javaee.mapper.ArticleMapper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static swu.edu.cn.javaee.component.Signal.*;

@Service
public class ArticleService {


    @Resource
    private ArticleMapper articleMapper;
    //用户增加文章
    public String saveArticle(String title,String tag,String author,String content,String description){
        Article article1 = new Article();
        Date creation_time=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        article1.setTitle(title);
        article1.setTag(tag);
        article1.setAuthor(author);
        article1.setContent(content);
        article1.setDescription(description);
        article1.setCreation_time(format.format(creation_time));
        articleMapper.saveArticle(article1);
        System.out.println(article1);
        return SUCCESS_CODE;
    }


    public ArticleInfo queryListLimit(String page,String limits){
        Integer start=Integer.valueOf(page);
        Integer limit=Integer.valueOf(limits);
        ArticleInfo articleInfo=new ArticleInfo();
        int count=articleMapper.countArticle();
        List<Article> list=articleMapper.queryListLimit((start-1)*limit,limit);
        if (list.size()!=0){
            articleInfo.setCode("0");
            articleInfo.setCount(String.valueOf(count));
            articleInfo.setMsg("");
            articleInfo.setData(list);
            System.out.println(articleInfo);
        }
        else {
            articleInfo.setCode("1");
        }
        return articleInfo;

    }


    //查看所有文章信息
    public ArticleInfo queryList(){
        List<Article> list=articleMapper.queryList();
        Integer count=articleMapper.countArticle();
        ArticleInfo articleInfo=new ArticleInfo();
        if(list.size()!=0){
            articleInfo.setCode("0");
            articleInfo.setCount(String.valueOf(count));
            articleInfo.setMsg("");
            articleInfo.setData(list);
            return articleInfo;
        }
        return null;
    }

    //按title查询
    public List<Article> queryArticleByTitle(String title){
        List<Article> articleList=articleMapper.queryArticleByTitle(title);
        System.out.println(articleList);
        return articleList;
    }

    //User_query
    public List<Article> queryArticleByAuthor(String author){
        List<Article> articleList=articleMapper.queryArticleByAuthor(author);
        System.out.println(articleList);
        return articleList;
    }

    //query by tag
    public List<Article> queryArticleByTag(String tag){
        List<Article> articleList =articleMapper.queryArticleByTag(tag);
        System.out.println(articleList);
        return articleList;
    }

    //用户updateArticle
    public String updateArticleByUser(Article article){
        Article result= articleMapper.queryArticleById(article.getNew_id());
        if (result!=null){
            result.setTitle(article.getTitle());
            result.setAuthor(article.getAuthor());
            result.setContent(article.getContent());
            result.setDescription(article.getDescription());
            articleMapper.updateArticleByUser(result);
            System.out.println(result);
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_Article_NO_EXIST;
        }
    }

    //管理员updateArticle
    public String updateArticleByAdmin(String new_id,String audit_flag,String auditor,String opinion,String publish_time,String publish_not){
        Article result= articleMapper.queryArticleById(new_id);
        if (result!=null){
            result.setAudit_flag(audit_flag);
            result.setAuditor(auditor);
            result.setOpinion(opinion);
            result.setPublish_time(publish_time);
            result.setPublish_not(publish_not);
            articleMapper.updateArticleByAdmin(result);
            System.out.println(result);
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_Article_NO_EXIST;
        }
    }

    //删除文章
    public String deleteArticleById(String new_id){
        Article result= articleMapper.queryArticleById(new_id);
        if (result!=null){
            articleMapper.deleteArticleById(result.getNew_id());
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_Article_NO_EXIST;
        }
    }

}
